﻿using FigureDraw.Diagram;
using FigureDraw.Graphic;
using FigureDraw.Shapes.Effect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FigureDraw
{
    public class MyPicture
    {
        private List<MyShape> lstShape;
        
        private List<MyEffect> lstEffect;

        private List<MyShape> lstEffectedShape;

        public List<MyEffect> LstEffect
        {
            get { return lstEffect; }
            set { lstEffect = value; }
        }

        public MyPicture()
        {
            lstShape = new List<MyShape>();
            lstEffect = new List<MyEffect>();
            lstEffectedShape = new List<MyShape>();
        }

        public void Add(MyShape shape)
        {
            if (shape != null)
                lstShape.Add(shape);
        }

        public void Draw(CommonGraphic graphic)
        {
            UpdateEffect();
            if(lstEffect.Count > 0)
                graphic.DrawPicture(lstEffectedShape);
            else
                graphic.DrawPicture(lstShape);                          
        }

        public void Clear()
        {
            lstShape.Clear();
            lstEffect.Clear();
            lstEffectedShape.Clear();
        }

        public void ConvertTo(BlockDiagram diagram)
        {
            List<MyShape> lstConvertShape = new List<MyShape>();

            foreach (var shape in lstShape)
                lstConvertShape.Add(shape.Clone(diagram));

            lstShape = lstConvertShape;
        }

        public void SwitchEffect(MyEffect addedEffect)
        {
            bool isAttach = true;

            foreach (var effect in lstEffect)
            {
                if (effect.GetType() == addedEffect.GetType())
                {
                    isAttach = false;
                    break;
                }
            }

            if (isAttach)
                AttachEffect(addedEffect);
            else
                DetachEffect(addedEffect);

            UpdateEffect();
        }

        private void AttachEffect(MyEffect effect)
        {        
            //List<MyShape> lstEffectedShape = new List<MyShape>();
            lstEffect.Add(effect);

            //foreach (var shape in lstShape)
            //    lstEffectedShape.Add(effect.AttachEffect(shape));

            //lstShape = lstEffectedShape;
        }

        private void DetachEffect(MyEffect effect)
        {
            //List<MyShape> lstEffectedShape = new List<MyShape>();
            
            //foreach (var shape in lstShape)
            //    lstEffectedShape.Add(shape.DetachEffect());

            //lstShape = lstEffectedShape;
            //foreach (var type in lstEffectType)
            //{ 
            //    if(type == effect.GetType())

            //}

            for (int i = 0; i < lstEffect.Count; i++)
            {
                if (lstEffect[i].GetType() == effect.GetType())
                    lstEffect.RemoveAt(i);
            }
        }

        private void UpdateEffect()
        {
            lstEffectedShape.Clear();
            foreach (var shape in lstShape) 
            {
                MyShape temp = shape.Clone();
                //MyShape temp = shape;
                foreach (var effect in lstEffect)
                    temp = effect.AttachEffect(temp);

                lstEffectedShape.Add(temp.Clone());
            }

        }
    }
}
